package com.mao.interview.lock;

/**
 * 有问题！有BUG 在while自旋中添加yeild似乎可以解决，但这是否又与CLH队列模型相悖
 * 
 * @author 54353
 *
 */
public class MyLockTest {
	static int count = 0;

	public static void main(String[] args) {
		MyClhLock lock = new MyClhLock();
		lock.lock();

		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				for (int i = 0; i < 20000; i++) {
					lock.lock();
					count++;
					lock.unlock();
					/*
					 * try { Thread.sleep(100); } catch (InterruptedException e)
					 * { e.printStackTrace(); return; }
					 */
					lock.lock();
					count++;
					lock.unlock();
				}
			}
		};
		Thread[] threads = new Thread[5];

		for (int i = 0; i < 5; i++) {
			threads[i] = new Thread(runnable);
			threads[i].start();
		}
		lock.unlock();

		for (int i = 0; i < 5; i++) {
			try {
				threads[i].join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println(count);
	}
}
